package com.icss.trans.accept.httppush;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TransServlet extends HttpServlet {

    protected static Logger logger = LoggerFactory.getLogger(TransServlet.class);

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TransServlet() {
        super();
    }

    public void doPush(PushParam pushParam) {
        logger.debug("PushParam====" + pushParam.toString());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @SuppressWarnings("unchecked")
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (!isMultipart) {
            response.getWriter().write("the enctype must be multipart/form-data");
            response.setStatus(HttpStatus.SC_BAD_REQUEST);
            return;
        }

        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> items;
        try {
            items = upload.parseRequest(request);
            PushParam pushParam = parseItems(items);
            doPush(pushParam);
        } catch (FileUploadException e) {
            logger.error("FileUploadException" + e.getMessage(), e);
        }
    }

    /**
     * @Description:
     * @param pushParam
     * @param items
     * @throws IOException
     */
    private PushParam parseItems(List<FileItem> items) throws IOException {
        PushParam pushParam = new PushParam();
        Iterator<FileItem> itr = items.iterator();
        while (itr.hasNext()) {
            FileItem item = (FileItem) itr.next();
            if (item.isFormField()) {
                doFormFieldItem(pushParam, item);
            } else {
                doFileFieldItem(pushParam, item);
            }
        }
        return pushParam;
    }

    /**
     * @Description:
     * @param pushParam
     * @param item
     * @throws IOException
     */
    private void doFileFieldItem(PushParam pushParam, FileItem item) throws IOException {
        String fieldName = item.getFieldName();
        if (fieldName.equals("inputStream")) {
            pushParam.setInputStream(item.getInputStream());
        }
    }

    /**
     * @Description:
     * @param pushParam
     * @param item
     */
    private void doFormFieldItem(PushParam pushParam, FileItem item) {
        String fieldName = item.getFieldName();
        String value = item.getString();
        if (fieldName.equals("sourceOrgCode")) {
            pushParam.setSourceOrgCode(value);
        } else if (fieldName.equals("sourceAppCode")) {
            pushParam.setSourceAppCode(value);
        } else if (fieldName.equals("destAppCode")) {
            pushParam.setDestAppCode(value);
        } else if (fieldName.equals("dataTransType")) {
            pushParam.setDataTransType(value);
        } else if (fieldName.equals("fileName")) {
            pushParam.setFileName(value);
        }
    }

}
